/** These are the files that we need to import */
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * This is a class to handle Bottom panel in JPanel.
 * @author Code Trader
 * @version 1.0
 */
public class BottomPanel extends JPanel implements ActionListener {

	/**
	 * This is serialVersion id
	 */
	private static final long serialVersionUID=5234300503712042914L;
	
	/**
	 * This is labels in Bottom panel. 
	 */
	private final JLabel life, upperLabel, lowerLabel, middleLabel, fuel;
	
	/**
	 * This is button in Bottom panel. 
	 */
	private final JButton turnButton;
	
	/**
	 *  This is a object of Player
	 */
	private Player player;
	
	/**
	 * This is a object of Ship
	 */
	private Ship ship;
	
	/**
	 * This is a bar for showing fuel and life status
	 */
	private final JProgressBar fuelProgress, lifeProgress;
	
	/**
	 * This is location home 
	 */
	private String location_status = "home";
	
	/**
	 * This is a object of SpaceTraderMainGUI.
	 */
	private SpaceTraderMainGUI mainPanel;
//	private InfoPanel infoPanel;
	
	/** for 640 **/
	private static final int S_HUNDRED_F_Z = 640;
	
	/** for 137 **/
	private static final int O_HUNDRED_T_S = 137;
	
	/** for 102 **/
	private static final int O_HUNDRED_Z_T = 102;
	
	/** for 153 **/
	private static final int O_HUNDRED_F_T = 153;
	
	/** for 51 **/
	private static final int FIFTYONE = 51;

	/** for 570 **/
	private static final int F_HUNDRED_S_Z = 570;
	
	/** for 87 **/
	private static final int EIGHTYSEVEN = 87;
	
	/** for 56 **/
	private static final int FIFTYSIX = 56;
	
	/** for 14 **/
	private static final int FOURTEEN = 14;
	
	/** for 204 **/
	private static final int T_HUNDRED_Z_F = 204;
	
	/** for 104 **/
	private static final int O_HUNDRED_Z_F = 104;
	
	/** for 16 **/
	private static final int SIXTEEN = 16;
	
	/** for 536 **/
	private static final int F_HUNDRED_T_S = 536;
	
	/** for 85 **/
	private static final int EIGHTYFIVE = 85;
	
	/** for 20 **/
	private static final int TWENTY = 20;
	
	/** for 50 **/
	private static final int FIFTY = 50;
	
	/** for 300 **/
	private static final int T_HUNDRED = 300;
	
	/** for 24 **/
	private static final int TWENTYFOUR = 24;
	
	/** for 81 **/
	private static final int EIGHTYONE = 81;
	
	/** for 186 **/
	private static final int O_HUNDRED_E_S = 186;
	
	/** for 69 **/
	private static final int SIXTYNINE = 69;
	
	/** for 252 **/
	private static final int T_HUNDRED_F_T = 252;
	
	/** for 100 **/
	private static final int O_HUNDRED = 100;
	
	/** for 12 **/
	private static final int TWELVE = 12;
	
	/** for 97 **/
	private static final int NINETYSEVEN = 97;
	
	/** for 68 **/
	private static final int SIXTYEIGHT = 68;
	
	/** for 161 **/
	private static final int O_HUNDRED_S_O = 161;
	
	/** for ONE **/
	private static final int ONE = 1;

	/** for TWO **/
	private static final int TWO = 2;
	
	/** for THREE **/
	private static final int THREE = 3;
	
	/** for FOUR **/
	private static final int FOUR = 4;
	
	/** for FIVE **/
	private static final int FIVE = 5;

	/** for TEN **/
	private static final int TEN = 10;
	
	/**
	 * Create the panel.
	 */
	
	public BottomPanel() {
		super.setOpaque(false);
		super.setBounds(0, T_HUNDRED_F_T, S_HUNDRED_F_Z, O_HUNDRED_T_S);
		super.setLayout(null);
		
		fuelProgress = new JProgressBar();
		fuelProgress.setForeground(new Color(O_HUNDRED_Z_T, O_HUNDRED_F_T, FIFTYONE));
		fuelProgress.setBounds(F_HUNDRED_S_Z, EIGHTYSEVEN, FIFTYSIX, FOURTEEN);
		super.add(fuelProgress);
		
		lifeProgress = new JProgressBar();
		lifeProgress.setForeground(new Color(T_HUNDRED_Z_F, FIFTYONE, 0));
		lifeProgress.setBounds(F_HUNDRED_S_Z, O_HUNDRED_Z_F, FIFTYSIX, FOURTEEN);
		super.add(lifeProgress);
		
		fuel = new JLabel("Fuel");
		fuel.setForeground(new Color(O_HUNDRED_Z_T, FIFTYONE, 0));
		fuel.setFont(new Font("Palatino Linotype", Font.BOLD, SIXTEEN));
		fuel.setBounds(F_HUNDRED_T_S, EIGHTYFIVE, FIFTYSIX, SIXTEEN);
		super.add(fuel);
		
		life = new JLabel("Life");
		life.setForeground(new Color(O_HUNDRED_Z_T, FIFTYONE, 0));
		life.setFont(new Font("Palatino Linotype", Font.BOLD, SIXTEEN));
		life.setBounds(F_HUNDRED_T_S, O_HUNDRED_Z_F, FIFTYSIX, SIXTEEN);
		super.add(life);
		
		//totalMoney & spaceLeft @ marketplace, item
		upperLabel = new JLabel("Upper Label");
		upperLabel.setForeground(new Color(O_HUNDRED_Z_T, FIFTYONE, 0));
		upperLabel.setFont(new Font("Palatino Linotype", Font.BOLD, TWENTY));
		upperLabel.setBounds(O_HUNDRED_S_O, FIFTY, T_HUNDRED, TWENTYFOUR);
		super.add(upperLabel);
		
		lowerLabel = new JLabel("Lower Label");
		lowerLabel.setForeground(new Color(O_HUNDRED_Z_T, FIFTYONE, 0));
		lowerLabel.setFont(new Font("Palatino Linotype", Font.BOLD, TWENTY));
		lowerLabel.setBounds(O_HUNDRED_S_O, EIGHTYONE, T_HUNDRED, TWENTY);
		super.add(lowerLabel);
		
		//save @ info
		middleLabel = new JLabel("Middle Label");
		middleLabel.setHorizontalAlignment(SwingConstants.CENTER);
		middleLabel.setForeground(new Color(O_HUNDRED_Z_T, FIFTYONE, 0));
		middleLabel.setFont(new Font("Palatino Linotype", Font.BOLD, TWENTY));
		middleLabel.setBounds(O_HUNDRED_E_S, SIXTYNINE, T_HUNDRED_F_T, TWENTY);
		super.add(middleLabel);
		
		fuelProgress.setValue(O_HUNDRED);
		lifeProgress.setValue(O_HUNDRED);
		
		/** Singleton **/
		this.ship = (Ship) Singleton.INSTANCE.get("ship");
		this.player = (Player) Singleton.INSTANCE.get("player");
		/** Singleton **/
		
		turnButton = new JButton("");
		turnButton.setPressedIcon(new ImageIcon(
				BottomPanel.class.getResource("/image/turn_pressed.png")));
		turnButton.setSelectedIcon(new ImageIcon(
				BottomPanel.class.getResource("/image/turn_pressed.png")));
		turnButton.setIcon(new ImageIcon(
				BottomPanel.class.getResource("/image/turn.png")));
		turnButton.setBounds(TWELVE, SIXTYNINE, NINETYSEVEN, SIXTYEIGHT);
		turnButton.setSelectedIcon(new ImageIcon(
				SpaceTraderMainGUI.class.getResource("/image/turn_pressed.png")));
		turnButton.setActionCommand("turn");
		turnButton.setOpaque(false);
		turnButton.setContentAreaFilled(false);
		turnButton.setBorderPainted(false);
		turnButton.addActionListener(this);
		super.setLayout(null);
		super.add(turnButton);
	}
	
	/**
	 * Updates the panel with the appropriate updated information.
	 * 
	 * @param current_status ActionCommand String to indicate which panel it is in
	 */
	public void updateBottomPanel(String current_status) {
		String status = current_status;
		ship = (Ship) Singleton.INSTANCE.get("ship");
		player = (Player) Singleton.INSTANCE.get("player");
		if (current_status.equals("")) {
			status = location_status;
		}
		if (status.equals("home")) {
			upperLabel.setText("Player Name: " + player.getName());
			lowerLabel.setText("Ship Type: " + ship.getShipType().toString());
			upperLabel.setVisible(true);
			lowerLabel.setVisible(true);
			middleLabel.setVisible(false);
		} else if (status.equals("map")) {
			upperLabel.setText("Planet Name: " + ship.getCurrentPlanet().getName());
			lowerLabel.setText("Location: (" + 
			ship.getCurrentPlanet().getPoint().getX() +
			", " + ship.getCurrentPlanet().getPoint().getY() + ")");
			upperLabel.setVisible(true);
			lowerLabel.setVisible(true);
			middleLabel.setVisible(false);
		} else if (status.equals("item")) {
			upperLabel.setText("Total Money: " + ship.getMoney());
			lowerLabel.setText("Cargo Space Left: " +
			(ship.getShipType().getMaxCargoSpace() - ship.getCargoSpace()));
			upperLabel.setVisible(true);
			lowerLabel.setVisible(true);
			middleLabel.setVisible(false);
		} else if (status.equals("store")) {
			upperLabel.setText("Total Money: " + ship.getMoney());
			lowerLabel.setText("Cargo Space Left: " +
			(ship.getShipType().getMaxCargoSpace() - ship.getCargoSpace()));
			upperLabel.setVisible(true);
			lowerLabel.setVisible(true);
			middleLabel.setVisible(false);
		} else if (status.equals("info")) {
			middleLabel.setText("Press Menu To Save");
			upperLabel.setVisible(false);
			lowerLabel.setVisible(false);
			middleLabel.setVisible(true);
		}
		location_status = status;
		fuelProgress.setValue(Math.round(new Float(ship.getFuel())
				/ ship.getShipType().getMaxFuel() * O_HUNDRED));
		lifeProgress.setValue(Math.round(new Float(ship.getLife())
				/ ship.getShipType().getHullStrength() * O_HUNDRED));
	}

	/**
	 * to perform action listener
	 * @param e
	 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("turn")) {
			ship.setLife(ship.getLife() + 
					ship.hasNumberOfItems(Gadget.AUTOREPAIR) * FIVE);
			if (ship.getTempPlanet() != null){
				ship.moveToPlanet(ship.getTempPlanet());
				mainPanel.update();
			}
			mainPanel.makeAnEvent();
			player.addPoint();
			final int num = ((Integer) Singleton.INSTANCE.get("difficulty") + 1) * TEN;
			if (player.getPoint() % num == 0) {
				final Random rand = new Random();
				final int random = rand.nextInt(FOUR);
				int num2;
				String msg = null;
				switch (random) {
				case 0:
					num2 = player.getEngineer();
					num2 += 1;
					player.setEngineer(num2);
					msg = "You Got the +1 Engineer";
					break;
				case ONE:
					num2 = player.getFighter();
					num2 += 1;
					player.setFighter(num2);
					msg = "You Got the +1 Fighter";
					break;
				case TWO:
					num2 = player.getPilot();
					num2 += 1;
					player.setPilot(num2);
					msg = "You Got the +1 Pilot";
					break;
				case THREE:
					num2 = player.getTrader();
					num2 += 1;
					player.setTrader(num2);
					msg = "You Got the +1 Trader";
					break;
				default:
					break;
				}
				if (msg != null) JOptionPane.showMessageDialog(null, msg);
				mainPanel.updateBottomPanel();
				mainPanel.update();
			}
			checkShipHealth();
		}
	}
	
	/**
	 * to check Ship Health
	 */
	private void checkShipHealth() {
		if (ship.getLife() <= 0) {
			final int choice = JOptionPane.showConfirmDialog(
					null, "You lose, Do you wanna play again?");
			if (choice == JOptionPane.YES_OPTION) {
				mainPanel.restart();
				return;
			}
			if (choice == JOptionPane.NO_OPTION) {
				System.exit(0);
			}
		}
	}
	
	/**
	 * to set MainGUI
	 * @param m
	 */
	public void setMainGUI(SpaceTraderMainGUI m) {
		this.mainPanel = m;
	}
}
